home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 138_01 / oct84col.ddj < prev    next >
Text File  |  1985-11-13  |  18KB  |  581 lines

  1. .pl 63
  2. .po 0
  3. ..
  4. .. this article was prepared for the C/Unix Programmer's Notebook
  5. .. Copyright 1984 (c) Anthony Skjellum.  
  6. ..
  7. .. by A. Skjellum
  8. ..
  9. .he "C/Unix Programmer's Notebook"   for October, 1984 DDJ. 
  10.  
  11.  
  12.                                Introduction
  13.  
  14.     The  thrust of this entire column is to illustrate scientific  uses 
  15.  
  16. of  the C programming language by examples.  The aim is to present routines 
  17.  
  18. useful  in  conjunction  with scientific programming,  some  of  which  are 
  19.  
  20. general and others which implement specific numerical algorithms. There are 
  21.  
  22. specific audiences for which this column is intended.   The first  audience 
  23.  
  24. is  the  group of  die-hard programmers who refuse to change languages  and 
  25.  
  26. continue  to produce useful programs in outdated languages.   Not  only  is 
  27.  
  28. this  of greater expense to themselves,  but also cheats others by  locking 
  29.  
  30. them  into FORTRAN or other old-fashioned languages.   For them,  I want to 
  31.  
  32. illustrate the elegance and versatility of C.   The second audience  (which 
  33.  
  34. may  also  include  the first as a subset) are those  users  interested  in 
  35.  
  36. scientific applications but who may not have used C for this purpose.  This 
  37.  
  38. article  is intended to illustrate that C is completely acceptable for such 
  39.  
  40. purposes and the code shows how generally concepts can be presented.  (This 
  41.  
  42. article makes no attempt to survey scientific applications where C could be 
  43.  
  44. used but merely includes some non-trivial examples).   Finally,  for  those 
  45.  
  46. readers  who  don't  fit into the above  categories,  the  general  purpose 
  47.  
  48. routines will still prove interesting. 
  49.  
  50.     Three  programming  systems are presented.   The first is a set  of 
  51.  
  52. general  purpose subroutines which are designed to simplify the process  of 
  53.  
  54. user-program  interaction,  and  to provide a  straight-forward  means  for 
  55.  
  56. handling  erroneous  input.  The  input  mechanisms  are  not  particularly 
  57.  
  58. sophisticated, but emphasize structure in the user's program.  The routines 
  59.  
  60. make  range  checking  so automatic that the programmer has no  excuse  for 
  61.  
  62. omitting  such  checks,  regardless  of how 'quickly' a program  is  to  be 
  63.  
  64. completed.   Providing these routines to novice programmers in a  classroom 
  65.  
  66. environment  has  eliminated  a lot of frustration over using  the  scanf() 
  67.  
  68. function.
  69.  
  70.     The  second  and third programming systems  illustrate  Runge-Kutta 
  71.  
  72. integration.   The Runge-Kutta formalism is a standard numerical  technique 
  73.  
  74. for  handling the numerical integration of one or more first order ordinary 
  75.  
  76. differential  equations.   Interested readers may wish to consult the  book 
  77.  
  78. Numerical Analysis, by Richard L.  Burden et.  al. (Prindle, Weber, Schmidt 
  79.  
  80. publishers,  Second edition,  1981).  This is the source for the algorithms 
  81.  
  82. presented  in  the  code  and is also a  fine  reference  for  introductory 
  83.  
  84. numerical  methods.   The  choice of Runge-Kutta routines as  examples  was 
  85.  
  86. based on their widespread use in scientific work.
  87.  
  88.                              Acknowledgements
  89.  
  90.     The  general purpose library has received extensive use by  Caltech 
  91.  
  92. students during the past school year.   Thanks are due to Scott Lewicki who 
  93.  
  94. discovered a couple of minor details which caused major errors.
  95.  
  96.     The  Runge-Kutta  code  was developed by  Michael  J.  Roberts  and 
  97.  
  98. myself.   Mr.  Roberts  developed the major portion of the RKSYS  (multiple 
  99.  
  100. equations)  routines  as  a  project  for  Caltech's  Physics  20   course: 
  101.  
  102. Introduction to Computational Physics (Prof.  Geoffrey C. Fox, instructor).  
  103.  
  104. Approximately  sixty  man  hours  were spent  in  developing,  testing  and 
  105.  
  106. debugging this code.   Mr.  Roberts also wrote the original version of  the 
  107.  
  108. documentation for RKSYS included in modified form as Table III.
  109.  
  110.  
  111.                        GPR: General Purpose Routines
  112.  
  113.     The general purpose library consists of five subroutines.   Four of 
  114.  
  115. these  subroutines  deal with input.   The fifth is a simple  facility  for 
  116.  
  117. printing files to the console.  This latter routine is called display() and 
  118.  
  119. will be considered separately from the input functions.
  120.  
  121.     The  other routines are iinp(),  finp(),  sinp() and  cinq().   The 
  122.  
  123. first three provide integer, floating point, and string input respectively.  
  124.  
  125. The latter is a yes-no question processor.  The exact calling sequences for 
  126.  
  127. each of the routines is provided in Table I.
  128.  
  129. .pa
  130.                       ---------- Table I. ----------
  131.  
  132. 1.    int iinp(prompt,cflag,low,high);
  133.  
  134.     char *prompt: optional prompt string to be printed before input
  135.     char cflag:   checking flag: if non-zero, range checking is performed
  136.     int low
  137.     int high:     low, high are (inclusive) range checking values
  138.  
  139.     iinp() repeats input until a valid number is entered; the valid 
  140.     number is the function's return value.
  141.  
  142. 2.    double finp(prompt,cflag,low,high);
  143.  
  144.     char *prompt: optional prompt string to be printed before input
  145.     char cflag:   checking flag: if non-zero, range checking is performed
  146.     double low
  147.     double high:  low, high are (inclusive) range checking values
  148.  
  149.     finp() repeats input until a valid number is entered; the valid
  150.     number is the function's return value
  151.  
  152. 3.    len = sinp(prompt,string,length);
  153.  
  154.     int len:      length of entered string
  155.     char *prompt: optional prompt string to be printed before input
  156.     int length:   maximum length of input string
  157.  
  158.     sinp() ignores leading spaces.
  159.  
  160. 4.    retn = cinq(prompt);
  161.  
  162.     int retn:     1 --> 'Y' was typed, 0 --> 'N' was typed
  163.     char *prompt: optional prompt string to be printed before input
  164.  
  165. 5.    retn = display(fname);
  166.  
  167.     int retn:     0 --> success, -1 --> failure       
  168.     char *fname:  null-terminated name of file
  169.  
  170.     display() prints the specified file on the standard output device.
  171.  
  172.  
  173.                     ---------- End Table I. ----------
  174.  
  175. .pa
  176.  
  177.     Traditionally,  user  input consists of a sequence of lines such as 
  178.  
  179. the following sequence for inputting an integer: 
  180.  
  181.         #define MIN 10
  182.         #define MAX 100
  183.         ...
  184.  
  185.         int input;
  186.         ...
  187.  
  188.         while (1)    /* input loop */
  189.         {
  190.             printf("Enter input variable --> ");
  191.  
  192.             if (scanf("%u",&input) != 1)    /* get variable */
  193.             {
  194.                 drain();    /* drain spurious characters */
  195.                 continue;    /* skip range checks */
  196.             }
  197.  
  198.             /* do range checking */
  199.  
  200.             if ((input >= MIN) && (input <= MAX))
  201.                 break;        /* we are done */
  202.  
  203.             printf("\nNumber out of range\n");
  204.  
  205.         } /* keep looping until scanf() can read a variable */
  206.  
  207. Entering   this   sequence  repeatedly  can  be  rather  tedious   from   a 
  208.  
  209. programmatical  point of view,  so error checking is often  omitted.   This 
  210.  
  211. practice leads to programs which do not handle user mistakes intelligently.  
  212.  
  213. The  GPR routines allow the above sequence to be replaced by a single  line 
  214.  
  215. of code:
  216.  
  217.         input = iinp("Enter input variable -->",1,MIN,MAX);
  218.  
  219. Since  using  the GPR input functions is easier than  using  scanf(),  this 
  220.  
  221. variety  of  function should be well-received and can be used  in  lieu  of 
  222.  
  223. scanf() for most purposes.   A further advantage of these functions is that 
  224.  
  225. they unclutter the user's program.   More sophisticated checking will still 
  226.  
  227. need to be included explicitly: the input functions only do range checking.
  228.  
  229.     The  display() function was included to encourage users to  provide 
  230.  
  231. on-line help/documentation along with their programs.  This function allows 
  232.  
  233. users  to  print  out additional  text  whenever  appropriate.   With  this 
  234.  
  235. function, a trivial on-line help facility can be created; a help feature is 
  236.  
  237. almost always appropriate but usually is omitted.
  238.  
  239.     The  GPR routines may be found in Listing I.   The current list  is 
  240.  
  241. not  exhaustive  but  intended  only  to suggest  a  trend  for  additional 
  242.  
  243. routines.
  244.  
  245.                         RK4: Runge-Kutta Algorithm
  246.  
  247.     We  have investigated the general purpose library which  simplifies 
  248.  
  249. the  task  of  correct  input.   Now  we  want  to  consider  a  scientific 
  250.  
  251. application  of  C:   single  equation  Runge-Kutta  integration.    Before